package com.tangguo.vshop.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tangguo.vshop.domain.Customer;
import com.tangguo.vshop.domain.Mainbody;
import com.tangguo.vshop.domain.Product;
import com.tangguo.vshop.domain.User;
import com.tangguo.vshop.repository.CustomerRepository;
import com.tangguo.vshop.repository.MainbodyRepository;
import com.tangguo.vshop.repository.OrderRepository;
import com.tangguo.vshop.repository.ProductRepository;
import com.tangguo.vshop.repository.UserRepository;
import com.tangguo.vshop.service.AuthService;

@RestController
@RequestMapping("/data")
public class InitDataController {
	public static final String DEFAULT_MAINBODY_CODE="001";
	public String[]CUSTOMER_NAME = {"老百姓大药房","开心药房","益丰大药房"};
	public String[]CUSTOMER_HELPCODE = {"LBX","KX","YF"};
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private CustomerRepository customerRepository;
	@Autowired
	private ProductRepository productRepository;
	@Autowired
	private MainbodyRepository mainbodyRepository;
	@Autowired
	private OrderRepository orderRepository;
	@Autowired
	private AuthService authService;
	@RequestMapping("/init")
	public ResponseResult initData() throws IOException{
		clear();
		initMainBody();
		initUser();
		initCustomer();
		initProduct();
		return ResponseResult.ok("初始化数据成功");
	}

	private void initProduct() throws IOException {
		Mainbody mainbody = mainbodyRepository.findByCode(DEFAULT_MAINBODY_CODE);
		List<Product> productlist = new ArrayList<>();
		InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("init_product.data");
		BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
		String line = "";
		while((line = reader.readLine())!=null){
			String[] columns = line.split(",");
			Product product = new Product();
			product.setMainbody(mainbody);
			product.setName(columns[0].replaceAll("'","").trim());
			product.setHelpcode(columns[1].replaceAll("'","").trim());
			product.setManufacturer(columns[2].replaceAll("'","").trim());
			product.setSpec(columns[3].replaceAll("'", "").trim());
			product.setPrice(19.9);
			product.setMarketPrice(29.8);
			product.setDescription("自动导入");
			product.setCode(columns[1].replaceAll("'", "").trim());
			product.setImages(new ArrayList<>());
			product.getImages().add(columns[4].replaceAll("'", "").trim());
			productlist.add(product);
		}
		reader.close();
		productRepository.insert(productlist);
	}

	private void initCustomer() {
		Mainbody mainbody = mainbodyRepository.findByCode(DEFAULT_MAINBODY_CODE);
		for(int i=0;i<CUSTOMER_NAME.length;i++){
			Customer customer = new Customer();
			customer.setName(CUSTOMER_NAME[i]);
			customer.setHelpcode(CUSTOMER_HELPCODE[i]);
			customer.setMainbody(mainbody);
			customer.setProvince("江西");
			customer.setCity("宜春");
			customer.setTown("樟树");
			List<String> businessScope = new ArrayList<>();
			businessScope.add("中药");
			businessScope.add("西药");
			
			List<String> forbidArea = new ArrayList<>();
			forbidArea.add("南昌");
			forbidArea.add("九江");
			customer.setForbidArea(forbidArea );
			customer.setBusinessScope(businessScope );
			customerRepository.save(customer);
		}
	}

	private void initUser() {
		User user = new User();
		Mainbody mainbody = mainbodyRepository.findByCode(DEFAULT_MAINBODY_CODE);
		user.setMainbody(mainbody);
		user.setUsername("doonly");
		user.setPassword("123456");
		authService.register(user);
	}

	private void initMainBody() {
		Mainbody mainbody = new Mainbody();
		mainbody.setCode(DEFAULT_MAINBODY_CODE);
		mainbody.setName("江西唐果科技有限公司");
		mainbody.setDescription("江西唐果科技有限公司是江西省一家专业从事软件开发、软件定制、软件实施的高新技术企业。公司网址：www.tangguoit.com!");
		mainbodyRepository.save(mainbody);
	}

	private void clear() {
		userRepository.deleteAll();
		customerRepository.deleteAll();
		productRepository.deleteAll();
		mainbodyRepository.deleteAll();
		orderRepository.deleteAll();
	}
}
